//
//  LBShowCaseCell.m
//  LBShowCase
//
//  Created by mac on 2024/6/29.
//

#import "LBShowCaseCell.h" 
#define kScreenWidth \
([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)] ? [UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale : [UIScreen mainScreen].bounds.size.width)
#define kScreenHeight \
([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)] ? [UIScreen mainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale : [UIScreen mainScreen].bounds.size.height)
#define kScreenSize \
([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)] ? CGSizeMake([UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale,[UIScreen mainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale) : [UIScreen mainScreen].bounds.size)



@interface LBShowCaseCell ()

@property (nonatomic, strong) UIImageView *imgView;

@end

@implementation LBShowCaseCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.contentView.clipsToBounds = YES;
        [self.contentView addSubview:self.imgView];
    }
    return self;
}

- (void)cellOnTableView:(UITableView *)tableView didScrollView:(UIView *)view
{
    CGFloat topSpace = tableView.frame.origin.y;
    CGRect  rect = [self convertRect:self.contentView.frame toView:view];
       //旧的图片Frame
       CGRect imageRect = self.imgView.frame;
       //移动
       if (rect.origin.y > topSpace && rect.origin.y<imageRect.size.height-self.contentView.frame.size.height + topSpace) {
           imageRect.origin.y = - CGRectGetMinY(rect) + topSpace;
       }else if (rect.origin.y>imageRect.size.height-self.contentView.frame.size.height + topSpace){
           imageRect.origin.y = -(imageRect.size.height - self.contentView.frame.size.height);
       }else if(rect.origin.y< topSpace){
           imageRect.origin.y = 0;
       }
       //新的图片Frame
       self.imgView.frame = imageRect;
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (UIImageView *)imgView
{
    if (!_imgView) {
        _imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,kScreenWidth , 0)];
        UIImage *image = [UIImage imageNamed:@"lgx"];
        CGFloat height = kScreenWidth * image.size.height / image.size.width;
        _imgView.image = image;
        _imgView.frame = CGRectMake(0, 0, kScreenWidth, height);
    }
    return _imgView;
}

@end
